# encoding:utf-8
import asyncio
import time
from pyppeteer.launcher import launch

async def scroll_page(page):
    """
    滚动浏览器页面
    :param page: 页面对象
    :return:
    """
    # ScrollHeight: 滚动高度
    totalHeight = await page.evaluate("""()=>{
            return document.body.scrollHeight;
            }""")
    scrollHeight = 1
    while totalHeight >= scrollHeight:
        if scrollHeight % 30 == 0:
            await page.waitFor(5000)
        await page.evaluate("""()=>{window.scrollTo(0, %s);}""" % scrollHeight)
        scrollHeight += 3

async def main():
    browser = await launch({
        'headless': False, # 显示窗口
        'dumpio': True,
        'args': [
            '--disable-extensions',
            '--hide-scrollbars',
            '--disable-bundled-ppapi-flash',
            '--mute-audio',
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-gpu',
            "--window-size=1500,900"
        ],
    })
    page = await browser.newPage() # "通过 Browser 对象创建页面 Page 对象"
    await page.setViewport({"width": 1920, "height": 1080})  # 改变 页面大小
    await page.goto('https://segmentfault.com/a/1190000013978236')
    time.sleep(3)

    await scroll_page(page=page)

    await browser.close()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())